package com.quanyan.assets.web;

import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.assets.entity.CouponInfoVo;
import com.quanyan.assets.entity.vo.*;
import com.quanyan.assets.request.ReqCoupon;
import com.quanyan.assets.response.RespOrderCoupon;
import com.quanyan.assets.service.CouponService;
import com.quanyan.assets.service.UserAssetsService;
import com.quanyan.assets.service.UtilCouponService;
import com.quanyan.common.enums.BaseEnum;
import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.ValidationResult;
import com.quanyan.common.utils.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * Created by wmb on 2016/10/13.
 */
@RestController
@RequestMapping("/api/assets/coupon")
public class CouponController {
    @Autowired
    private UserAssetsService userAssetsService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private UtilCouponService utilCouponService;

    //获取订单使用优惠券列表
    @RequestMapping(value="/getOrderCouponList",method = RequestMethod.POST)
    public APIResponse<RespOrderCoupon> getOrderCouponList(@RequestBody ReqCoupon reqCoupon,HttpServletRequest request){
        if(null==request.getAttribute("uid")){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        reqCoupon.setUid((Integer)request.getAttribute("uid"));
    //    reqCoupon.setStatus(CouponStatus.RECEIVE.id.byteValue());
        APIRequest apiRequest=new APIRequest();
        apiRequest.setData(reqCoupon);
        apiRequest.setVer(reqCoupon.getVer());
        return couponService.getOrderCouponList(apiRequest);
    }

    //领取礼包
    @RequestMapping(value="/receiveCoupons", method = RequestMethod.POST)
    @Deprecated
    public APIResponse<BaseEnum> grantCoupons(HttpServletRequest request) throws ParseException {
        Integer uid = (Integer) request.getAttribute("uid");
        if(null == uid){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return utilCouponService.grantCoupons(uid);
    }

    //领取礼包
    @RequestMapping(value="/receiveCoupons2", method = RequestMethod.POST)
    @Deprecated
    public APIResponse<BaseEnum> grantCoupons2(HttpServletRequest request) throws ParseException {
        Integer uid = (Integer) request.getAttribute("uid");
        if(null == uid){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return utilCouponService.grantCoupons2(uid);
    }

    // 根据优惠券模板领取优惠券
    @RequestMapping(value="/obtainCoupons", method = RequestMethod.POST)
    public APIResponse<CouponInfoVo> obtainCoupons(@RequestBody ObtainCouponsVo vo, HttpServletRequest request) throws ParseException {
        Integer uid = (Integer) request.getAttribute("uid");
        if(null == uid){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        ValidationResult result = ValidationUtils.validateEntity(vo);
        if (result.hasErrors()) {
            throw new BizException(ApiResponseEnum.PARAMETER_INVALID, result.getErrorDesc());
        }
        String couponTemplateCode = vo.getCouponTemplateCode();
        return utilCouponService.obtainCoupons(couponTemplateCode, uid);
    }

    // 根据优惠券码领取优惠券
    @RequestMapping(value="/obtainCouponsByCouponCode", method = RequestMethod.POST)
    public APIResponse<CouponInfoVo> obtainCouponsByCouponCode(@RequestBody ObtainCouponsByCouponCodeVo vo, HttpServletRequest request) throws ParseException {
        Integer uid = (Integer) request.getAttribute("uid");
        if(null == uid){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        ValidationResult result = ValidationUtils.validateEntity(vo);
        if (result.hasErrors()) {
            throw new BizException(ApiResponseEnum.PARAMETER_INVALID, result.getErrorDesc());
        }
        String couponCode = vo.getCouponCode();
        return utilCouponService.obtainCouponsByCouponCode(couponCode, uid);
    }

    // 根据优惠券码获取优惠券信息
    @RequestMapping(value="/getCouponInfo", method = RequestMethod.POST)
    public APIResponse<CounponInfoRespVo> getCouponInfo(@RequestBody GetCouponInfoVo vo) throws ParseException {
        ValidationResult result = ValidationUtils.validateEntity(vo);
        if (result.hasErrors()) {
            throw new BizException(ApiResponseEnum.PARAMETER_INVALID, result.getErrorDesc());
        }
        String couponCode = vo.getCouponCode();
        return utilCouponService.getCouponInfo(couponCode);
    }

    // 获取活动优惠券例表
    @RequestMapping(value="/getActivityCouponList", method = RequestMethod.POST)
    public APIResponse<RespActivityCouponListVo> getActivityCouponList(@RequestBody ReqActivityCouponListVo vo, HttpServletRequest request) throws ParseException {
        Integer uid = (Integer) request.getAttribute("uid");
        ValidationResult result = ValidationUtils.validateEntity(vo);
        if (result.hasErrors()) {
            throw new BizException(ApiResponseEnum.PARAMETER_INVALID, result.getErrorDesc());
        }
        Byte activityType = vo.getActivityType();
        return utilCouponService.getActivityCouponList(activityType, uid);
    }


}
